<?php
namespace app\admin\controller;
use app\service\apiinvoiceService;
use app\service\apixcxService;
use app\service\appletService;
use app\service\homeService;
use app\service\lifeapiService;
use app\service\webService;
use app\service\xcxService;
use printing\printDemo;
use think\Controller;
use think\facade\Db;
use think\facade\View;

class H5alipay extends Base{
	public function test()
	{
		session("url",request()->url());
	}
	//验证姓名
	public function checkname(){
		if(request()->isPost())
		{
			$data=request()->post();
			$roominfo=Db::table("roominfo")->where("id",$data["room_id"])->find();
			
			if(strpos($roominfo["owner_name"],$data["owner_name"])!== false){
				$roominfo_user=Db::table("roominfo_user")->where("user_id",session("user_id"))->where("room_id",$data["room_id"])->find();
				/*$url=url("billinfo_discount")."?id=".$roominfo["id"];
				echo "<script>location.href='$url';</script>";
				exit;*/
				if(empty($roominfo_user))
				{
					$communityinfo=Db::table("community")->where("community_id",$roominfo["community_id"])->find();
					$info["user_id"]=session("user_id");
					$info["room_id"]=$data["room_id"];
					$info["community_id"]=$roominfo["community_id"];
					$info["community_name"]=$communityinfo["community_name"];
					$info["address"]=$roominfo["address"];
					$info["create_time"]=time();
					$res=Db::table("roominfo_user")->insert($info);
				}
				$url=url("billinfo")."?id=".$roominfo["id"];
				echo "<script>location.href='$url';</script>";
				exit;
				
			}else{
				echo "<script>alert('验证失败');history.go(-1);</script>";
				exit;
			}
		}else{
			$id=request()->get("id");
			$roominfo_user=Db::table("roominfo_user")->where("user_id",session("user_id"))->where("room_id",$id)->find();
			if(!empty($roominfo_user)){
				$url=url("billinfo")."?id=".$id;
				echo "<script>location.href='$url';</script>";
				exit;
			}
			$roominfo=Db::table("roominfo")->where("id",$id)->find();
			if(mb_strlen($roominfo["owner_name"])=="1"){
				$url=url("billinfo")."?id=".$roominfo["id"];
				echo "<script>location.href='$url';</script>";
				exit;
			}
			if(empty($roominfo["owner_name"])){
				$url=url("billinfo")."?id=".$roominfo["id"];
				echo "<script>location.href='$url';</script>";
				exit;
			}
			$roominfo["owner_name"]=str_repeat('*', mb_strlen($roominfo['owner_name'])-1).mb_substr($roominfo["owner_name"],-1);
			View::assign("roominfo",$roominfo);
			return View::fetch("check_name");
		}
		
	}
	public function selectroom(){
		//session("url",request()->url());
		/*$roominfo_user=Db::table("roominfo_user")->where("user_id",session('user_id'))->find();
		if(!empty($roominfo_user))
		{
			$url=url("H5alipay/billinfo_discount")."?id=".$roominfo_user["room_id"];
			echo "<script>location.href='$url';</script>";
			exit;
		}*/
		$community_id=request()->get("community_id");
		$communityinfo=Db::table("community")->where("id",$community_id)->find();
		if(empty($communityinfo)){
		    return "小区信息错误";
			$this->error("小区信息错误");
		}
		View::assign("communityinfo",$communityinfo);

		$group = (new xcxService())->groupList($communityinfo["community_id"]);
		
		$str="[";
		foreach ($group as $key => $value) {
			$str=$str."'".$value."',";
		}
		$str=$str."]";
		
		View::assign("str",$str);
		return View::fetch();
	}
    
    public function getgroup(){
		$community_id = request()->param('community_id');
        $communityinfo=Db::table("community")->where("community_name",$community_id)->find();
        $community_id=$communityinfo["id"];
        $group = (new xcxService())->groupList($community_id);
        sort($group ,SORT_NATURAL|SORT_STRING|SORT_FLAG_CASE);
        return $group ;
	}
    
	public function getbuilding(){
		$community_id = request()->param('community_id');
        $group = request()->param('group');;
        $building = (new xcxService())->buildingList($group,$community_id);
        sort($building,SORT_NATURAL|SORT_STRING|SORT_FLAG_CASE);
        return $building;
	}
	public function getunit(){
		$community_id = request()->param('community_id');
        $group = request()->param('group');;
        $building = request()->param('building');
        $unit = (new xcxService())->unitList($building,$group,$community_id);
        sort($unit,SORT_NATURAL|SORT_STRING|SORT_FLAG_CASE);
        return $unit;
	}
	public function getroom(){
		$community_id = request()->param('community_id');
        $group = request()->param('group');;
        $building = request()->param('building');
        $unit = request()->param('unit');
        $room = (new xcxService())->roomList($unit,$building,$group,$community_id);
        sort($room,SORT_NATURAL|SORT_STRING|SORT_FLAG_CASE);
        return $room;
	}

	public function getroominfo(){
		$community_id = request()->param('community_id');
        $group = request()->param('group');;
        $building = request()->param('building');
        $unit = request()->param('unit');
        $room = request()->param('room');
        $roominfo=(new xcxService())->getroominfo($community_id,$group,$building,$unit,$room);
        if(empty($roominfo)){
        	$arr["errorCode"]="1";
        	$arr["errorMsg"]="请选择房屋";
        	echo json_encode($arr);
        	exit;
        }else{
        	$arr["errorCode"]="0";
        	$arr["roominfo"]=$roominfo;
        	echo json_encode($arr);
        	exit;
        }
	}

	public function billinfo(){
		session("url",request()->url());
		$id=request()->get("id");
		$roominfo=Db::table("roominfo")->where("id",$id)->find();
		$communityinfo=Db::table("community")->where("community_id",$roominfo["community_id"])->find();
		
		$roominfo["community_name"]=$communityinfo["community_name"];

		 //根据房号查询账单
        $billlis = (new appletService())->billRoomIdList($roominfo['out_room_id']);
        if(empty($billlis)){
			return View::fetch("nopaymsg");
		}
		//查询缴费类型
		$costtype = (new appletService())->cost_type($roominfo['out_room_id']);
		$billsum=(new appletService())->billsum($roominfo['out_room_id']);
		$billlist=array();
        foreach ($costtype as $b){
        	$arr["costtype"]=$b;
            $billinfo = (new appletService())->cost_type_bill($roominfo['out_room_id'],$b);
            $arr["billinfo"]=$billinfo;
            $billlist[]=$arr;
        }
        
		/*echo "<pre>";
		var_dump($billlist);
		die;*/
		View::assign("billsum",$billsum);
		View::assign("billlist",$billlist);
		View::assign("roominfo",$roominfo);

        $notifyurl_alipay=(new webService())->getNotifyurlAlipay();
        View::assign("notifyurl_alipay",$notifyurl_alipay);
		return View::fetch();
	}



	//计算要缴费的金额
	public function moneysum(){
		$post=request()->post();
		$ordermoney=0;
		if(!(request()->has('billid'))){
			return json(['state'=>'1','info' => "0"]);
		}
		foreach ($post["billid"] as $key => $value) {
			$billinfo=Db::table("bill")->where('id',$value)->find();
			$ordermoney=$ordermoney+$billinfo["bill_entry_amount"];
		}
		$data["order_money"]=$ordermoney;
		
		
		$data["pay_money"]=round($data["order_money"],2);
		return json(['state'=>'1','info' => $data["pay_money"]]);
	}

	public function createOrder(){
		if(!(request()->has('billid'))){
			return json(['info' => '请选择要交费的账单']);
		}
		$post=request()->post();
		$billpay["billid"]=serialize($post["billid"]);
		
		$ordermoney=0;
		$subjectremark="";
		foreach ($post["billid"] as $key => $value) {
			$billinfo=Db::table("bill")->where('id',$value)->find();
			if($billinfo["status"]=="TRADE_SUCCESS"){
				return json(['info' => '该帐单已缴费']);
			}
			$ordermoney=$ordermoney+$billinfo["bill_entry_amount"];
			
			$subjectremark=$subjectremark.$billinfo["cost_type"].":".$billinfo["bill_entry_amount"]."元| ";
		}
		$billpay["order_money"]=$ordermoney;
		$billpay["user_id"]=session("user_id");
		
		$billpay["out_room_id"]=$post["out_room_id"];
		$billpay["room_address"]=$post["room_address"];
		$billpay["community_id"]=$post["community_id"];
		$billpay["community_name"]=$post["community_name"];
		
		$billpay["pay_money"]=round($billpay["order_money"],2);
		$billpay["out_trade_no"]=date('YmdHiss').rand(100000,999999);

		$token = (new xcxService())->token($billpay["community_id"]);
        $subject = $billpay["community_name"].$billpay["room_address"].$subjectremark.'已缴费';
        $api = new lifeapiService();
         
            
       
        $notifyUrl="https://www.wuye.zfbzhsq.com/admin/callback/alinotice";
        $result = $api->orderCreate(json_encode(['out_trade_no'=> $billpay["out_trade_no"],'total_amount'=>$billpay["pay_money"],'buyer_id'=>$billpay["user_id"],'subject' =>$subject,'token'=>$token ,'notifyurl'=>$notifyUrl]));
		if(property_exists($result,'sub_msg')){	
			return json(['info' => $result->sub_msg]);
		}else{
			$billpay['trade_no'] =  $result->trade_no;
			
			$billpay["scenes"]=3;	
			$billpay["subject"]=$subject;
			$res=Db::table("billpay")->insert($billpay);
			//返回结果到前台唤起收银台
			if($res){
				return json(['order_no' => $billpay['trade_no']]);
			}else{
				return json(['info' => '订单存储失败']);
			}
		}	
	}

	public function billinfo_discount(){
		session("url",request()->url());
		$id=request()->get("id");
		$roominfo=Db::table("roominfo")->where("id",$id)->find();
		$communityinfo=Db::table("community")->where("community_id",$roominfo["community_id"])->find();
		
		$roominfo["community_name"]=$communityinfo["community_name"];

		 //根据房号查询账单
        $billlis = (new appletService())->billRoomIdList($roominfo['out_room_id']);
        if(empty($billlis)){
			return View::fetch("nopaymsg");
		}
		//查询缴费类型
		$costtype = (new appletService())->cost_type($roominfo['out_room_id']);
		$billsum=(new appletService())->billsum($roominfo['out_room_id']);
		if($communityinfo["discount_status"]=="1")
		{

			$billsum1=Db::name('bill')
							->where('out_room_id',$roominfo['out_room_id'])
							->where('status','WAIT_PAYMENT')
							->where('discount_status','1')
							->sum('bill_entry_amount');
			$billsum2=Db::name('bill')
							->where('out_room_id',$roominfo['out_room_id'])
							->where('status','WAIT_PAYMENT')
							->where('discount_status','2')
							->sum('bill_entry_amount');
			$billsum=round($billsum1*$communityinfo["discount_rate"],1)+$billsum2;
		}
		$billlist=array();
        foreach ($costtype as $b){
        	$arr["costtype"]=$b;
            $billinfo = (new appletService())->cost_type_bill($roominfo['out_room_id'],$b);
            $arr["billinfo"]=$billinfo;
            $billlist[]=$arr;
        }

        $roominfo['owner_name']=str_repeat('*', mb_strlen($roominfo['owner_name'])).mb_substr($roominfo['owner_name'], -1) ;
		View::assign("billsum",$billsum);
		View::assign("billlist",$billlist);
		View::assign("roominfo",$roominfo);
		View::assign("communityinfo",$communityinfo);
		return View::fetch("billinfo_discount");
	}
	//计算要缴费的金额
	public function moneysum_discount(){
		$post=request()->post();
		$ordermoney=0;
		if(!(request()->has('billid'))){
			return json(['state'=>'1','info' => "0"]);
		}
		//
		$community_id=request()->post("community_id");
		$communityinfo=Db::table("community")->where("community_id",$community_id)->find();
		foreach ($post["billid"] as $key => $value) {
			$billinfo=Db::table("bill")->where('id',$value)->find();
			$money=$billinfo["bill_entry_amount"];
			if(($communityinfo["discount_status"]=="1")&&($billinfo["discount_status"]=="1"))
			{
				$money=round($billinfo["bill_entry_amount"]*$communityinfo["discount_rate"],1);
			}
			$ordermoney=$ordermoney+$money;
		}
		$data["order_money"]=$ordermoney;
		
		
		$data["pay_money"]=round($data["order_money"],2);
		return json(['state'=>'1','info' => $data["pay_money"]]);
	}

	public function createOrder_discount(){
		if(!(request()->has('billid'))){
			return json(['info' => '请选择要交费的账单']);
		}
		$post=request()->post();
		$billpay["billid"]=serialize($post["billid"]);

		$community_id=request()->post("community_id");
		$communityinfo=Db::table("community")->where("community_id",$community_id)->find();
		

		$ordermoney=0;
		$discount_billid=array();
		foreach ($post["billid"] as $key => $value) {
			$billinfo=Db::table("bill")->where('id',$value)->find();
			if($billinfo["status"]=="TRADE_SUCCESS"){
				return json(['info' => '该帐单已缴费']);
			}
			$money=$billinfo["bill_entry_amount"];
			if(($communityinfo["discount_status"]=="1")&&($billinfo["discount_status"]=="1"))
			{
				$money=round($billinfo["bill_entry_amount"]*$communityinfo["discount_rate"],1);
				$arr["discount_status"]=1;
				$arr["money"]=$money;
				$arr["discount_rate"]=$communityinfo["discount_rate"];
				$discount_billid[$value]=$arr;
			}else{
				$arr["discount_status"]=2;
				$arr["money"]="";
				$arr["discount_rate"]="";
				$discount_billid[$value]=$arr;
			}
			$ordermoney=$ordermoney+$money;
		}
		$billpay["order_money"]=$ordermoney;
		$billpay["user_id"]=session("user_id");
		$billpay["discount_billid"]=json_encode($discount_billid);
		$billpay["out_room_id"]=$post["out_room_id"];
		$billpay["room_address"]=$post["room_address"];
		$billpay["community_id"]=$post["community_id"];
		$billpay["community_name"]=$post["community_name"];
		
		$billpay["pay_money"]=round($billpay["order_money"],2);
		$billpay["out_trade_no"]=date('YmdHiss').rand(100000,999999);

		$token = (new xcxService())->token($billpay["community_id"]);
        $subject = $billpay["community_name"].$billpay["room_address"].'物业缴费';
        $api = new apixcxService();
        $notifyUrl="https://www.jfzhiwen.com/admin/callback/alinotice_discount";
        $result = $api->create(json_encode(['out_trade_no'=> $billpay["out_trade_no"],'total_amount'=>$billpay["pay_money"],'buyer_id'=>$billpay["user_id"],'subject' =>$subject,'token'=>$token ,'notifyUrl'=>$notifyUrl]));
		if(property_exists($result,'sub_msg')){	
			return json(['info' => $result->sub_msg]);
		}else{
			$billpay['trade_no'] =  $result->trade_no;
			
			$billpay["scenes"]=3;	
			$res=Db::table("billpay")->insert($billpay);
			//返回结果到前台唤起收银台
			if($res){
				return json(['order_no' => $billpay['trade_no']]);
			}else{
				return json(['info' => '订单存储失败']);
			}
		}	
	}
	
	//收银台收款
	public function cashierpay(){
	    session("url",request()->url());
	    if(request()->isPost()){
	        $id=request()->post("id");
	        $cashierinfo=Db::table("cashier")->where("id",$id)->find();
	        if($cashierinfo["status"]=="1"){
	            return json(['info' => '订单已支付']);
	        }
	        $token = (new xcxService())->token($cashierinfo["community_id"]);
	        $communityinfo=Db::table("community")->where("community_id",$cashierinfo["community_id"])->find();
            $subject = $communityinfo["community_name"]."收银台收费";
            $api = new lifeapiService();
            $notifyUrl="https://www.wuye.zfbzhsq.com/admin/callback/cashierNotice";
            $result = $api->orderCreate(json_encode(['out_trade_no'=> $id,'total_amount'=>$cashierinfo["money"],'buyer_id'=>session("user_id"),'subject' =>$subject,'token'=>$token ,'notifyurl'=>$notifyUrl]));
    		if(property_exists($result,'sub_msg')){	
    			return json(['info' => $result->sub_msg]);
    		}else{
    			$info['trade_no'] =  $result->trade_no;
    			$res=Db::table("cashier")->where("id",$id)->update(["trade_no"=>$result->trade_no]);
    			//返回结果到前台唤起收银台
    			if($res){
    				return json(['order_no' => $info['trade_no']]);
    			}else{
    				return json(['info' => '订单存储失败']);
    			}
    		}	
	    }else{
	        $community_id=request()->get("community_id");
	        $communityinfo=Db::table("community")->where("id",$community_id)->find();
	        if(empty($communityinfo)){
	            return "小区信息错误";
	        }
	        $cashierinfo=Db::table("cashier")->where("community_id",$communityinfo["community_id"])->where("status","2")->find();
	        if(empty($cashierinfo)){
	            return View::fetch("cashier_nomsg");
	        }
	        View::assign("communityinfo",$communityinfo);
	        View::assign("cashierinfo",$cashierinfo);

            $notifyurl_alipay=(new webService())->getNotifyurlAlipay();
            View::assign("notifyurl_alipay",$notifyurl_alipay);
	        return View::fetch("cashier_pay");
	    }
	}
	    
	public function gatepay2(){
	    //session("url",request()->url());
	    
	    if(request()->isPost()){
	        $gateid=request()->post("gateid");
    	    $gateinfo=Db::table("property_parking_gate")->where("id",$gateid)->find();
    	    
	        $info["gateid"]=$gateinfo["id"];
	        $info["property_id"]=$gateinfo["property_id"];
	        $info["gateName"]=$gateinfo["gateName"];
    		$info["user_id"]=session("user_id");
	        $info["order_money"]=request()->post("pay_money");
	        $info["remark"]=request()->post("remark");
    		$info["user_id"]=session("user_id");
    		$info["community_name"]=$gateinfo["community_name"];
    		$info["pay_money"]=request()->post("pay_money");
    		$info["out_trade_no"]=date('YmdHiss').rand(100000,999999);
            $info["subject"] = $gateinfo["community_name"]."-".$gateinfo["gateName"];
    		$token = Db::name('property')->where('id',$gateinfo["property_id"])->value('accredit');;
            $api = new lifeapiService();
            $notifyUrl="https://www.wuye.zfbzhsq.com/admin/callback/gatepayNotice";
            $result = $api->orderCreate(json_encode(['out_trade_no'=> $info["out_trade_no"],'total_amount'=>$info["pay_money"],'buyer_id'=>$info["user_id"],'subject' =>$info["subject"],'token'=>$token ,'notifyurl'=>$notifyUrl]));
	        if(property_exists($result,'sub_msg')){	
    			return json(['info' => $result->sub_msg]);
    		}else{
    			$info['trade_no'] =  $result->trade_no;
    			$res=Db::table("billpark")->insert($info);
    			//返回结果到前台唤起收银台
    			if($res){
    				return json(['order_no' => $info['trade_no']]);
    			}else{
    				return json(['info' => '订单存储失败']);
    			}
    		}	
	        
	    }else{
	        $gateid=request()->get("gateid");
    	    $gateinfo=Db::table("property_parking_gate")->where("id",$gateid)->find();
    	    View::assign("gateinfo",$gateinfo);
    	    return View::fetch();
	    }
	}
	
	public function gatepay(){
	    //session("url",request()->url());
	    
	    if(request()->isPost()){
	        $gateid=request()->post("gateid");
    	    $gateinfo=Db::table("property_parking_gate")->where("id",$gateid)->find();
    	    
	        $info["gateid"]=$gateinfo["id"];
	        $info["property_id"]=$gateinfo["property_id"];
	        $info["gateName"]=$gateinfo["gateName"];
    		$info["user_id"]=session("user_id");
	        $info["order_money"]=request()->post("pay_money");
	        $info["remark"]=request()->post("remark");
    		$info["user_id"]=session("user_id");
    		$info["community_name"]=$gateinfo["community_name"];
    		$info["pay_money"]=request()->post("pay_money");
    		$info["out_trade_no"]=date('YmdHiss').rand(100000,999999);
            $info["subject"] = $gateinfo["community_name"]."-".$gateinfo["gateName"];
    		$propertyinfo= Db::name('property')->where('id',$gateinfo["property_id"])->find();
    		$token=$propertyinfo["parking_accredit"];
            $api = new apiinvoiceService();
            $notifyUrl="https://www.wuye.zfbzhsq.com/admin/callback/gatepayNotice";
            $serviceinfo=Db::table("service")->where("id",$propertyinfo["service_id"])->find();
            $is_support_invoice="false";
            if($serviceinfo["is_support_invoice"]=="1"){
                $is_support_invoice="true";
            }
		    $invoice_merchant_name=$serviceinfo["invoice_merchant_name"];
		    $tax_num=$serviceinfo["fp_saler_tax_num"];
            $result = $api->createinvoice(json_encode(['out_trade_no'=> $info["out_trade_no"],'total_amount'=>$info["pay_money"],'buyer_id'=>$info["user_id"],'subject' =>$info["subject"],'token'=>$token ,'notifyurl'=>$notifyUrl,'is_support_invoice'=>$is_support_invoice,'invoice_merchant_name'=>$invoice_merchant_name,'tax_num'=>$tax_num]));
	        if(property_exists($result,'sub_msg')){	
    			return json(['info' => $result->sub_msg]);
    		}else{
    			$info['trade_no'] =  $result->trade_no;
    			$res=Db::table("billpark")->insert($info);
    			//返回结果到前台唤起收银台
    			if($res){
    				return json(['order_no' => $info['trade_no']]);
    			}else{
    				return json(['info' => '订单存储失败']);
    			}
    		}	
	        
	    }else{
	        $gateid=request()->get("gateid");
    	    $gateinfo=Db::table("property_parking_gate")->where("id",$gateid)->find();
    	    View::assign("gateinfo",$gateinfo);
            $notifyurl_alipay=(new webService())->getNotifyurlAlipay();
            View::assign("notifyurl_alipay",$notifyurl_alipay);
    	    return View::fetch();
	    }
	}
	

	
	public function communitycashierselectroom(){
	    $cashier_id=request()->get("cashier_id");
	    $cashierinfo=Db::table("community_cashier")->where("id",$cashier_id)->find();
		$communityinfo=Db::table("community")->where("id",$cashierinfo["community_id"])->find();
		if(empty($communityinfo)){
		    return "小区信息错误";
			$this->error("小区信息错误");
		}
		View::assign("cashierinfo",$cashierinfo);
		View::assign("communityinfo",$communityinfo);

		$group = (new xcxService())->groupList($communityinfo["community_id"]);
		
		$str="[";
		foreach ($group as $key => $value) {
			$str=$str."'".$value."',";
		}
		$str=$str."]";
		
		View::assign("str",$str);
		return View::fetch("community_cashier_selectroom");
	}
	
	public function communitycashierpay(){
	    session("url",request()->url());
		$id=request()->get("id");
		$cashier_id=request()->get("cashier_id");
		$roominfo=Db::table("roominfo")->where("id",$id)->find();
		$communityinfo=Db::table("community")->where("community_id",$roominfo["community_id"])->find();
		
		$roominfo["community_name"]=$communityinfo["community_name"];

		 //根据房号查询账单
        $billlis = (new appletService())->billRoomIdList($roominfo['out_room_id']);
        if(empty($billlis)){
			return View::fetch("nopaymsg");
		}
		//查询缴费类型
		$costtype = (new appletService())->cost_type($roominfo['out_room_id']);
		$billsum=(new appletService())->billsum($roominfo['out_room_id']);
		$billlist=array();
        foreach ($costtype as $b){
        	$arr["costtype"]=$b;
            $billinfo = (new appletService())->cost_type_bill($roominfo['out_room_id'],$b);
            $arr["billinfo"]=$billinfo;
            $billlist[]=$arr;
        }
        
		/*echo "<pre>";
		var_dump($billlist);
		die;*/
		View::assign("billsum",$billsum);
		View::assign("cashier_id",$cashier_id);
		View::assign("billlist",$billlist);
		View::assign("roominfo",$roominfo);
		return View::fetch("community_cashier_pay");
	}
	
	
	public function cashiercreateOrder(){
		if(!(request()->has('billid'))){
			return json(['info' => '请选择要交费的账单']);
		}
		$post=request()->post();
		$billpay["billid"]=serialize($post["billid"]);
		
		$ordermoney=0;
		$subjectremark="";
		foreach ($post["billid"] as $key => $value) {
			$billinfo=Db::table("bill")->where('id',$value)->find();
			if($billinfo["status"]=="TRADE_SUCCESS"){
				return json(['info' => '该帐单已缴费']);
			}
			$subjectremark=$subjectremark.$billinfo["cost_type"].":".$billinfo["bill_entry_amount"]."元| ";
			$ordermoney=$ordermoney+$billinfo["bill_entry_amount"];
		}
		$billpay["order_money"]=$ordermoney;
		$billpay["user_id"]=session("user_id");
		
		$billpay["cashier_id"]=$post["cashier_id"];
		$billpay["out_room_id"]=$post["out_room_id"];
		$billpay["room_address"]=$post["room_address"];
		$billpay["community_id"]=$post["community_id"];
		$billpay["community_name"]=$post["community_name"];
		
		$billpay["pay_money"]=round($billpay["order_money"],2);
		$billpay["out_trade_no"]=date('YmdHiss').rand(100000,999999);

		$token = (new xcxService())->token($billpay["community_id"]);
        $subject = $billpay["community_name"].$billpay["room_address"].$subjectremark.'交房已付款';
        $api = new lifeapiService();
         
            
       
        $notifyUrl="https://www.wuye.zfbzhsq.com/admin/callback/communitycashiernotice";
        $result = $api->orderCreate(json_encode(['out_trade_no'=> $billpay["out_trade_no"],'total_amount'=>$billpay["pay_money"],'buyer_id'=>$billpay["user_id"],'subject' =>$subject,'token'=>$token ,'notifyurl'=>$notifyUrl]));
		if(property_exists($result,'sub_msg')){	
			return json(['info' => $result->sub_msg]);
		}else{
			$billpay['trade_no'] =  $result->trade_no;
			$billpay["subject"]=$subject;
			$billpay["scenes"]=3;	
			$res=Db::table("billpay")->insert($billpay);
			//返回结果到前台唤起收银台
			if($res){
				return json(['order_no' => $billpay['trade_no']]);
			}else{
				return json(['info' => '订单存储失败']);
			}
		}	
	}
}